
/****************************************************************************/
/*Copyright (c) 2011, Florent DEVILLE.                                      */
/*All rights reserved.                                                      */
/*                                                                          */
/*Redistribution and use in source and binary forms, with or without        */
/*modification, are permitted provided that the following conditions        */
/*are met:                                                                  */
/*                                                                          */
/* - Redistributions of source code must retain the above copyright         */
/*notice, this list of conditions and the following disclaimer.             */
/* - Redistributions in binary form must reproduce the above                */
/*copyright notice, this list of conditions and the following               */
/*disclaimer in the documentation and/or other materials provided           */
/*with the distribution.                                                    */
/* - The names of its contributors cannot be used to endorse or promote     */
/*products derived from this software without specific prior written        */
/*permission.                                                               */
/* - The source code cannot be used for commercial purposes without         */ 
/*its contributors' permission.                                             */
/*                                                                          */
/*THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       */
/*"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT         */
/*LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS         */
/*FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE            */
/*COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,       */
/*INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,      */
/*BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;          */
/*LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER          */
/*CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT        */
/*LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN         */
/*ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE           */
/*POSSIBILITY OF SUCH DAMAGE.                                               */
/****************************************************************************/

#pragma once

//base class
#include "RTIPrimitive.h"

namespace RT
{

	//This class represent a triangle
	class RTPrimTriangle : public RTIPrimitive
	{
	public:

		//the determinant used to solve the uv map
		F32 _D;

		//flag set to indicate if a translation is needed during uv map solving
		bool _useTranslation;

		//the three poI32s translated
		RTVector3f _uvTranslated[3];

		//the translation vector used to offset
		RTVector3f _offset;

	private:
		//the tree vertex composing the triangle
		RTVector3f m_vertex[3];

		//The three uv map coordinate (x = U, y = V, w = nothing)
		RTVector3f m_UV[3];

		//Triangle normal
		RTVector3f m_normal;

	
	public:
		//constructor
		RTPrimTriangle();

		//constructor
		RTPrimTriangle(RTVector3f* v);

		//constructor
		RTPrimTriangle(RTVector3f v1, RTVector3f v2, RTVector3f v3);

		//destructor
		~RTPrimTriangle();

		//add a vertex
		void addVertex(const RTVector3f& v, I32 id);

		//add a uv texture coordinate
		void addUV(const RTVector3f& v, I32 id);

		//set the normal
		void setNormal(const RTVector3f& n);

		//calculate the normal
		void computeNormal();

		//precompute some value to make calculation faster
		void preCompute();

		//calculate if there is an intersection between ray and the triangle
		bool intersect(const RTRay& ray, F32& dist, bool useGlobalCoordinates);

		//calculate if there is an intersection between the ray and the triangle. The ray must be in triangle local space.
		bool intersectLocalSpace(const RTRay& ray, F32& dist);

		/*Calculate the triangle's normal*/
		void computeNormal(const RTRay& ray, F32 dist, RTVector3f& normal)const;

		/*Check if the point is in the triangle*/
		bool isInside(const RTVector3f& p)const;

		//get a pointer to the vertex array
		RTVector3f* getVertices();

		//get a pointer to the uv map array
		RTVector3f* getUV();

	private:
		/*Check if p1 is on the same side as the triangle normal*/
		bool sameSide(const RTVector3f& p1, const RTVector3f& p2, const RTVector3f& a, const RTVector3f& b)const;

	};
}